<template>
  <div>
    <div class="pageheader">
      <XHeader :left-options="{backText:''}" :title="headerTitle" class="XHeader">
        <div slot="right" style="color: #351009" @click="editTap">{{isedit?'完成':'编辑'}}</div>
      </XHeader>
    </div>
    <div class="page">
      <div style="  margin-top: 12px;">
        <div style="margin-top:12px;">
          <div class="block" v-for="(item,index) in products" :key="index">
            <div class="item" @click="onShpingDetai(item)" style="padding: 0 12px 0 0;">
              <label class="inputChecked" @click.stop>
                <input
                  type="checkbox"
                  class="check"
                  :disabled="isedit?false:!item.active"
                  :value="index"
                  v-model="checks"
                />
              </label>
              <chimage class="image" :src="item.goods_image_main" mode="aspectFill"></chimage>
              <div class="content">
                <div style="flex:1">
                  <div class="title">{{item.goods_name}}</div>
                  <div class="price">￥{{item.goods_price}}</div>
                </div>
                <div class="quantity" v-if="item.active" @click.stop>
                  <div class="quantity-num">起订量 {{item.goods_moq}}</div>
                  <div style="display: flex">
                    <div class="minus" @click="onMinus(item)">-</div>
                    <div class="num">{{item.goods_number}}</div>
                    <div class="plus" @click="onPlus(item)">+</div>
                  </div>
                </div>
                <div class="quantity" v-if="item.sku_name && item.sku_name.length">
                  <div class="app-specs">规格: {{specsName(item.sku_name)}}</div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <load-more
          v-if="!more&&!products.length"
          :show-loading="false"
          :tip="'暂无数据'"
          background-color="#fbf9fe"
        ></load-more>
      </div>
      <div class="block settlebox">
        <div class="settlebar">
          <div>
            <label class="selectall">
              <input class="check" type="checkbox" name="selectall" :checked="checkAll" @click="onCheckAll"/>
              <span>全选</span>
            </label>
          </div>
          <template v-if="!isedit">
            <div class="total">
              合计：
              <span class="money">￥{{(total+0.00000001).toFixed(2)}}</span>
            </div>
            <button class="settle" @click="onClearing">结算</button>
          </template>
          <template v-if="isedit">
            <button class="settle" @click="delCart">删除</button>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import Me from '@/dll/me'
  import specs from '@/mixin/specs'

  export default {
    name: 'shopping',
    mixins: [specs],
    data () {
      return {
        xheader: {
          title: '购物车'
        },
        products: [],
        checks: [],
        checkAll: false,
        total: 0,
        more: true,
        isedit: false
      }
    },
    computed: {
      headerTitle () {
        if (this.goodsNum !== 0) {
          return `购物车(${this.goodsNum})`
        } else {
          return `购物车`
        }
      },
      // 判断输入框数量是否小于起订量
      isGoodsMoq () {
        return (item) => {
          return parseFloat(item.goods_number) <= parseFloat(item.goods_moq)
        }
      }
    },
    activated () {
      this.init()
      this.GET_GOODS_NUM()
    },
    watch: {
      checks () {
        if (this.products.length == 0) {
          return
        }
        let length = 0
        this.products.map(item => {
          if (item.active) {
            length = length + 1
          }
        })
        if (this.checks.length == length) {
          this.checkAll = true
        } else {
          this.checkAll = false
        }
        this.onTotal()
      }
    },
    methods: {
      init () {
        this.total = 0
        this.checkAll = false
        this.checks = []
        this.products = []
        this.more = true
        this.getCartProducts()
      },
      editTap () {
        this.total = 0
        this.checkAll = false
        this.checks = []
        this.isedit = !this.isedit
      },
      // 减少数量
      onMinus (item) {
        if (parseFloat(item.goods_number) <= parseFloat(item.goods_moq)) {
          this.$vux.toast.text('不能小于起订量')
        } else {
          item.goods_number--
          this.editToCart(item)
          this.onTotal()
        }
      },
      // 添加数量
      onPlus (item) {
        if (item.supplier_id == -1 || item.goods_number < item.goods_storage) {
          item.goods_number++
          this.editToCart(item)
          this.onTotal()
        } else {
          this.$vux.alert.show({
            title: '提示',
            content: '库存不足,无法增加。'
          })
        }
      },
      // 全选
      onCheckAll () {
        let checks = []
        let checkAll = !this.checkAll
        if (checkAll) {
          let items = this.products
          items.map((item, index) => {
            if (this.isedit) {
              checks.push(index)
            } else {
              if (item.active) {
                checks.push(index)
              }
            }
          })
        }
        this.checks = checks
        this.checkAll = checkAll
      },
      // 合计
      onTotal () {
        let products = this.products
        let checks = this.checks
        let total = 0
        checks.map(item => {
          let product = products[item]
          total = total + product.goods_price.replace(/,/g, '') * product.goods_number
        })
        this.total = total
      },
      // 结算
      onClearing () {
        if (!this.checks.length) {
          this.$vux.toast.text('请选择需要结算的商品')
          return
        }
        for (let i = 0; i < this.checks.length; i++) {
          if (parseFloat(this.products[this.checks[i]].goods_number) < parseFloat(this.products[this.checks[i]].goods_moq)) {
            this.$vux.alert.show({
              title: '提示',
              content: `所选商品购买数量必须大于起订量`
            })
            return
          }
        }
        let items = []
        this.checks.map(item => {
          items.push({
            cart_id: this.products[item].cart_id,
            goods_number: this.products[item].goods_number
          })
        })
        this.$router.push({
          name: 'mallClearing',
          query: { items: JSON.stringify(items), if_cart: 1 }
        })
      },
      // 获取购物车列表
      async getCartProducts () {
        this.$vux.loading.show({
          text: '加载中'
        })
        let res = await Me.getCartGoods()
        this.$vux.loading.hide()
        if (res.code == 0) {
          this.products = res.data
        }
        this.more = false
      },
      // 修改购物车
      async editToCart (item) {
        let param = {
          cart_id: item.cart_id,
          goods_number: item.goods_number
        }
        await Me.editCartGoods(param)
      },
      // 删除购物车
      async delCart () {
        if (!this.checks.length) {
          return
        }
        let cart_ids = []
        this.checks.map(item => {
          cart_ids.push(this.products[item].cart_id)
        })
        this.$vux.loading.show({
          text: '请稍后'
        })
        let res = await Me.delCartGoods(cart_ids)
        this.$vux.loading.hide()
        if (res.code == 0) {
          this.GET_GOODS_NUM()
          this.$vux.alert.show({
            title: '提示',
            content: '删除成功'
          })
          this.init()
        } else {
          this.$vux.alert.show({
            title: '提示',
            content: res.msg
          })
        }
      },
      onShpingDetai (item) {
        this.$router.push({ name: 'mallInfo', params: { id: item.goods_sku } })
      }
    }
  }
</script>

<style lang="less" scoped>
  .page {
    padding-bottom: 72px;
  }

  .item {
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 12px 16px;
    background-color: #fff;
  }

  .inputChecked {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 12px 20px;
    height: 100px;
  }

  .check {
    display: block;
    height: 16px;
    width: 16px;
    margin: 0;
    zoom: 125%;
  }

  input[checkbox] .wx-checkbox-input {
    display: block;
    box-sizing: border-box;
    height: 16px;
    width: 16px;
    border-radius: 0;
  }

  input[checkbox] .wx-checkbox-input.wx-checkbox-input-checked::before {
    color: #ae2309;
    font-size: 15px;
    font-weight: 600;
  }

  .item .image {
    width: 100px;
    height: 100px;
    display: block;
    /*margin-left: 12px;*/
    background-color: #f5f5f5;
  }

  .item .content {
    flex: 1;
    margin-left: 16px;
    display: flex;
    height: 80px;
    flex-direction: column;
  }

  .item .content .title {
    font-size: 12px;
    color: #9f8d89;
    line-height: 17px;
    height: 34px;
    font-weight: 500;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
    overflow: hidden;
  }

  .item .content .price {
    font-size: 14px;
    color: #ae2309;
    font-weight: 600;
    line-height: 16px;
  }

  .item .content .quantity {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }

  .item .content .quantity .plus {
    display: block;
    width: 24px;
    height: 24px;
  }

  .item .content .quantity .minus,
  .item .content .quantity .plus {
    display: block;
    width: 24px;
    height: 24px;
    line-height: 22px;
    text-align: center;
    font-size: 16px;
    box-sizing: border-box;
    border: 1px solid #d4d4d4;
  }

  .item .content .quantity .num {
    display: block;
    width: 40px;
    height: 24px;
    line-height: 24px;
    font-size: 18px;
    color: #ae2309;
    text-align: center;
    overflow: auto;
  }

  .settlebox {
    box-sizing: border-box;
    padding: 12px;
    background-color: #fbf8f7;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }

  .settlebar {
    padding-left: 16px;
    display: flex;
    background-color: #fff;
    align-items: center;
    justify-content: space-between;
  }

  .settlebar .selectall {
    display: flex;
    align-items: center;
    font-size: 16px;
    color: #351009;
    line-height: 22px;
  }

  .settlebar .total {
    font-size: 16px;
    color: #351009;
    line-height: 22px;
    margin-right: 12px;
  }

  .settlebar .money {
    color: #ae2309;
    font-weight: 600;
  }

  .settlebar .settle {
    width: 88px;
    height: 48px;
    line-height: 48px;
    font-size: 16px;
    background-color: #ae2309;
  }

  .quantity-num {
    font-size: 12px;
    color: #9f8d89;
  }
</style>
